package com.ihunanren.biz.function;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Sets;

public class TextMather {

	public static String getMatherString(String text,String regex){
		checkArgument(!isNullOrEmpty(text) && !isNullOrEmpty(regex),"text and regex is required");
		
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(text);
		while(matcher.find()){
			return matcher.group(0);
		}
		return null;
	}
	
	public static Set<String> getMatherArray(String text,String regex){
		checkArgument(!isNullOrEmpty(text) && !isNullOrEmpty(regex),"text and regex is required");
		
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(text);
		Set<String> set = Sets.newHashSet();
		while(matcher.find()){
			set.add(matcher.group());
		}
		return set;
	}
}
